package com.zggk.iroad.mvp;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.gyf.immersionbar.ImmersionBar;
import com.jaeger.library.StatusBarUtil;
import com.zggk.iroad.R;
import com.zggk.iroad.api.MyAppliCation;

/**
 * 作者 by wuliang 时间 16/10/31.
 * <p>
 * 所有activity的基类，此处建立了一个activity的栈，用于管理activity
 */

public abstract class BaseActivity extends AppCompatActivity {
    protected Toast mToast;// 消息提示Toast
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(getLayout());
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        StatusBarUtil.setTransparent(this);
        MyAppliCation.self.addActivity(this);
        if (!this.isTaskRoot()) {
            Intent intent = getIntent();
            if (intent != null) {
                String action = intent.getAction();
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
                    finish();
                    return;
                }
            }
        }
    }


    /**
     * 常用的跳转方法
     */
    public void gotoActivity(Class<?> cls, boolean isFinish) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
        if (isFinish) {
            finish();
        }
    }

    public void gotoActivity(Class<?> cls, Bundle bundle, boolean isFinish) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(bundle);
        startActivity(intent);
        if (isFinish) {
            finish();
        }
    }

    /**
     * 设置返回
     */
    protected void goBack() {
        ImageView imageView = findViewById(R.id.back);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 设置标题
     *
     * @param title
     */
    protected void setTitleText(String title) {
        TextView titleTex = findViewById(R.id.title_text);
        titleTex.setText(title);
    }

    /**
     * 弹出消息提示
     *
     * @param msg      消息文本
     * @param duration 显示时长，长或短
     * @param gravity  弹出位置，上、中、下
     */
    protected void showToast(String msg, int duration, int gravity) {
        cancelToast();
        if (this.isFinishing() || TextUtils.isEmpty(msg)) {
            return;
        }
        mToast = Toast.makeText(this, msg, duration);
        mToast.setGravity(gravity, 0, gravity == Gravity.BOTTOM ? 50 : 0);
        mToast.show();
    }

    /**
     * 取消提示
     */
    protected void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
    }
    protected abstract int getLayout();
    @Override
    protected void onDestroy() {
        MyAppliCation.self.removeActivity(this);
        cancelToast();
        super.onDestroy();
    }
}
